package cn.goktech.sports.modules.contentManage.service.impl;

import cn.goktech.sports.common.entity.Page;
import cn.goktech.sports.common.entity.Query;
import cn.goktech.sports.common.entity.R;
import cn.goktech.sports.common.utils.CommonUtils;
import cn.goktech.sports.modules.contentManage.dao.MessageMapper;
import cn.goktech.sports.modules.contentManage.entity.MessageEntity;
import cn.goktech.sports.modules.contentManage.entity.dto.MessageAddDTO;
import cn.goktech.sports.modules.contentManage.service.BusinessTypeService;
import cn.goktech.sports.modules.contentManage.service.MessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 消息内容表
 *
 * @author duanyu<>
 */
@Service("messageService")
public class MessageServiceImpl implements MessageService {

	@Autowired
	private MessageMapper messageMapper;

	@Autowired
	private BusinessTypeService businessTypeService;

	/**
	 * 分页查询
	 *
	 * @param params
	 * @return
	 */
	@Override
	public Page<MessageEntity> listMessage(Map<String, Object> params) {
		Query query = new Query(params);
		Page<MessageEntity> page = new Page<>(query);
		messageMapper.listForPage(page, query);
		return page;
	}

	/**
	 * 新增
	 *
	 * @param message
	 * @return
	 */
	@Override
	public R saveMessage(MessageAddDTO message) {
		MessageEntity messageEntity = new MessageEntity();
		BeanUtils.copyProperties(message, messageEntity);
		messageEntity.setBusinessId(messageEntity.getBusinessId());
		messageEntity.setOrderNum(0);
		messageEntity.setStatus("0");
		messageEntity.setDelFlag("0");
		messageEntity.setCreateTime(new Date());
		int count = messageMapper.save(messageEntity);
		return CommonUtils.msg(count);
	}

	/**
	 * 根据id查询
	 *
	 * @param id
	 * @return
	 */
	@Override
	public R getMessageById(Long id) {
		MessageEntity message = messageMapper.getObjectById(id);
		return CommonUtils.msg(message);
	}

	/**
	 * 修改
	 *
	 * @param message
	 * @return
	 */
	@Override
	public R updateMessage(MessageEntity message) {
		message.setUpdateTime(new Date());
		int count = messageMapper.update(message);
		return CommonUtils.msg(count);
	}

	/**
	 * 删除
	 *
	 * @param id
	 * @return
	 */
	@Override
	public R batchRemove(Long[] id) {
		int count = messageMapper.batchRemove(id);
		return CommonUtils.msg(id, count);
	}

	@Override
	public R listByBusinessId(Long businessId) {
		List<MessageEntity> list = messageMapper.listByBusinessId(businessId);
		return CommonUtils.msg(list);
	}
}
